<template>
  <div class="import">
    <el-card style="text-align:center">
      <h3>{{ name }}导入</h3>
      <ReadExcel
        :onSuccess="onSuccess"
        :beforeUpload="beforeUpload"
      ></ReadExcel>
    </el-card>
  </div>
</template>

<script>
import { sysUserBatch } from '@/api/employees'
export default {
  name: 'Import',
  data () {
    return {
      name: this.$route.query.name
    }
  },
  methods: {
    beforeUpload (file) {
      console.log(file)
      return true
    },
    async onSuccess ({ header, results }) {
      // console.log(header, results)
      const obj = {
        手机号: 'mobile',
        姓名: 'username',
        入职日期: 'timeOffEntry',
        转正日期: 'correctionTime',
        工号: 'workNumber'
      }
      const newArr = results.map(item => {
        const temp = {}
        Object.keys(item).forEach(item2 => {
          temp[obj[item2]] = item[item2]
            if (
              obj[item2] === 'timeOffEntry' ||
              obj[item2] === 'correctionTime'
            ) {
              const t1 = new Date(
                (item[item2] - 1) * 24 * 60 * 60 * 1000 - 8 * 60 * 60 * 1000
              )
              t1.setYear(t1.getFullYear() - 70)
              temp[obj[item2]] = t1
            }
        })
        return temp
      })
      console.log('导入数据',newArr)
      await sysUserBatch(newArr)
      this.$message.success('导入成功')
      this.$router.push(this.$route.query.redirect || '/employees')
    }
  }
}
</script>

<style></style>
